﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LuKou.Member;
using LuKou.Member.Services;
using LuKou.Member.Domains;
using LuKou.Web.BLL.Extensions;

namespace LuKou.Web.BLL.Service
{
    public class UserService
    {
        private static IUserSvr _svr;
        static UserService()
        {
            _svr = ServiceHelper.Get<IUserSvr>("IUserSvr");
        }
        
        public static void ChangePassword(long ID, string oldPwd, string newPwd)
        {
            _svr.ChangePassword(ID, oldPwd, newPwd);
        }

        public static User GetUserInfoById(long ID)
        {
            return _svr.GetUserDetail(ID, true);
        }

        public static void ModifyUser(User data)
        {
            _svr.ModifyUser(data);
        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <returns></returns>
        public static PagedList<User> GetUserList(int index, int pageSize)
        {
            return GetUserList(null, index, pageSize, null);
        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <returns></returns>
        public static PagedList<User> GetUserList(EUserType? userType, int index, int pageSize)
        {
            return GetUserList(userType, index, pageSize, null);
        }

        public static int GetUserCount(EUserType? userType)
        {
            UserQueryParam param = new UserQueryParam();
            if (userType.HasValue)
            {
                param.AddParamOfUserType((int)userType);
            }
            return _svr.GetUserCount(param);
        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <returns></returns>
        public static PagedList<User> GetUserList(EUserType? userType, int index, int pageSize, IList<String> orderFields)
        {
            UserQueryParam param = new UserQueryParam();
            if (userType.HasValue)
            {
                param.AddParamOfUserType((int)userType);
            }

            if (orderFields != null && orderFields.Count > 0)
            {
                foreach (var orderField in orderFields)
                {
                    param.SetOrderByField(orderField);
                }
            }

            if (index < 1) index = 1;
            if (pageSize < 1) pageSize = 10;

            IList<User> userlist = _svr.GetUserList(param, true, (index - 1)*pageSize, pageSize).Cast<User>().ToList();

            return new PagedList<User>(userlist, index, pageSize, _svr.GetUserCount(param));
        }
    }
}
